package com.yzt.zhuju.utils;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.network.PersonalDetailAlbumDbEntity;
import com.yzt.zhuju.network.entity.PersonalDetailDbEntity;
import com.yzt.zhuju.network.entity.user.PersonalDetail;
import com.yzt.zhuju.network.entity.user.PersonalDetailAlbum;
import com.yzt.zhuju.network.entity.user.PersonalDetailBasic;
import com.yzt.p_base.AppUtils;

import org.litepal.LitePal;

import java.util.List;

/**
 * author : bob
 * date   : 2022/11/29
 * desc   :
 */
public class PersonalDetailCacheManager {

    /**
     * 插入或者更新用户详情缓存信息
     */
    public static boolean addPersonalDetail(PersonalDetailBasic personalDetail) {
        PersonalDetailDbEntity personalDetailDbEntity = LitePal.where("userId = '" + personalDetail.getUserId() + "' ").findFirst(PersonalDetailDbEntity.class);
        boolean isUpdate = true;
        if (personalDetailDbEntity == null) {
            personalDetailDbEntity = new PersonalDetailDbEntity();
            isUpdate = false;
        }
        personalDetailDbEntity.setUserId(personalDetail.getUserId());
        personalDetailDbEntity.setUpdateTime(System.currentTimeMillis());
        personalDetailDbEntity.setPersonalDetail(JSONObject.toJSONString(personalDetail));
        return isUpdate ? personalDetailDbEntity.updateAll("userId = ?", personalDetailDbEntity.getUserId()) > 0 : personalDetailDbEntity.save();
    }

    /**
     * 插入或者更新用户主页相册图片缓存信息
     */
    public static boolean addPersonalDetailAlbum(String userId, List<PersonalDetailAlbum> detailAlbums) {
        PersonalDetailDbEntity personalDetailDbEntity = LitePal.where("userId = '" + userId + "' ").findFirst(PersonalDetailDbEntity.class);
        boolean isUpdate = true;
        if (personalDetailDbEntity == null) {
            personalDetailDbEntity = new PersonalDetailDbEntity();
            isUpdate = false;
        }
        PersonalDetailAlbumDbEntity personalDetailAlbumDbEntity = new PersonalDetailAlbumDbEntity();
        personalDetailAlbumDbEntity.setUserId(userId);
        personalDetailAlbumDbEntity.setList(detailAlbums);
        personalDetailDbEntity.setUserId(userId);
        personalDetailDbEntity.setUpdateTime(System.currentTimeMillis());
        personalDetailDbEntity.setAlbumList(JSONObject.toJSONString(personalDetailAlbumDbEntity));
        return isUpdate ? personalDetailDbEntity.updateAll("userId = ?", personalDetailDbEntity.getUserId()) > 0 : personalDetailDbEntity.save();
    }

    /**
     * 获取用户缓存信息
     */
    public static PersonalDetail getPersonalDetail(String userId) {
        try {
            PersonalDetailDbEntity personalDetailDbEntity = LitePal.where("userId = '" + userId + "' ").findFirst(PersonalDetailDbEntity.class);
            if (personalDetailDbEntity == null || TextUtils.isEmpty(personalDetailDbEntity.getPersonalDetail())) {
                return null;
            }
            return JSONObject.parseObject(personalDetailDbEntity.getPersonalDetail(), PersonalDetail.class);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取用户缓存户主页相册图片信息
     */
    public static List<PersonalDetailAlbum> getPersonalDetailAlbum(String userId) {
        try {
            PersonalDetailDbEntity personalDetailDbEntity = LitePal.where("userId = '" + userId + "' ").findFirst(PersonalDetailDbEntity.class);
            if (personalDetailDbEntity == null || TextUtils.isEmpty(personalDetailDbEntity.getAlbumList())) {
                return null;
            }
            PersonalDetailAlbumDbEntity personalDetailAlbumDbEntity = JSONObject.parseObject(personalDetailDbEntity.getAlbumList(), PersonalDetailAlbumDbEntity.class);
            return personalDetailAlbumDbEntity == null ? null : personalDetailAlbumDbEntity.getList();
        } catch (Exception e) {
            SDKInitManager.initOtherSDk(AppUtils.getApp());
            return null;
        }
    }

    /**
     * 删除一周前的缓存数据
     */
    public static void deleteAWeekBefore() {
        LitePal.deleteAll(PersonalDetailDbEntity.class, "updateTime < ?", String.valueOf(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 7));
    }
}
